package gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import managers.Email;
import mappers.MemberMapper;
import domains.Member;

public class SendMailGUI {
	private JFrame sendMailFrame;
	private JTable table;
	private JPanel btnPanel, p, searchPanel, subjectPanel, bodyPanel, toPanel;
	private MyTableModel myTableModel;
	private JScrollPane scrollPane, bodyScrollPane, toScrollPane;
	private JButton sendBtn, cancelBtn;
	private JLabel searchLbl, subjectLbl, bodyLbl, toLbl;
	private JTextField searchTF, subjectTF;
	private JTextArea bodyArea, toArea;
	
	public SendMailGUI() {
			
		MemberMapper mMapper = new MemberMapper();
		List<Member> data = mMapper.selectAllMembers();
		myTableModel = new MyTableModel(data);
		table = new JTable(myTableModel);
		
		final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
				myTableModel);
		table.setRowSorter(sorter);
		
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					table = (JTable) e.getSource();
					int row = table.getSelectedRow();
					Object id = (Object) table.getValueAt(row, 3);
					toArea.append(id.toString()+ ", ");		
				}
			}
		});
		
		DefaultTableCellRenderer leftRenderer = new DefaultTableCellRenderer();
		leftRenderer.setHorizontalAlignment(JLabel.LEFT);
		table.getColumnModel().getColumn(0).setCellRenderer(leftRenderer);
		
		p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		toPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		subjectPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		bodyPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		
		searchLbl = new JLabel("Søg: ");
		searchTF = new JTextField(14);
		
		searchTF.getDocument().addDocumentListener(new DocumentListener() {

			private void searchFieldChangedUpdate(DocumentEvent evt) {
				String text = searchTF.getText();
				if (text.isEmpty()) {
					sorter.setRowFilter(null);
				} else {
					try {
						sorter.setRowFilter(RowFilter.regexFilter(
								"(?i)" + text, 0, 1, 2, 3));

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent evt) {
				searchFieldChangedUpdate(evt);			
			}

			@Override
			public void insertUpdate(DocumentEvent evt) {
				searchFieldChangedUpdate(evt);				
			}

			@Override
			public void removeUpdate(DocumentEvent evt) {
				searchFieldChangedUpdate(evt);
			}
		});
		
		searchPanel.add(searchLbl);
		searchPanel.add(searchTF);
		
		scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(new Dimension(500, 150));
		table.setFillsViewportHeight(true);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);	
		
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.getColumnModel().getColumn(0).setPreferredWidth(90);
		table.getColumnModel().getColumn(1).setPreferredWidth(115);
		table.getColumnModel().getColumn(2).setPreferredWidth(105);
		table.getColumnModel().getColumn(3).setPreferredWidth(200);
		
		toLbl = new JLabel("Modtager(e)");
		toPanel.add(toLbl);
		
		toArea = new JTextArea(2, 30);
		toArea.setLineWrap(true);
		toArea.setWrapStyleWord(true);
		toScrollPane = new JScrollPane(toArea);
		
		subjectLbl = new JLabel("Emne");
		subjectLbl.setFont(new Font("Arial", Font.BOLD, 18));
		
		subjectPanel.add(subjectLbl);
		
		subjectTF = new JTextField();
		subjectTF.setPreferredSize(new Dimension(100, 25));
		
		bodyLbl = new JLabel("Besked");
		bodyLbl.setFont(new Font("Arial", Font.BOLD, 16));
		bodyPanel.add(bodyLbl);
		
		bodyArea = new JTextArea(7, 50);
		bodyArea.setWrapStyleWord(true);
		bodyArea.setLineWrap(true);
		
		bodyScrollPane = new JScrollPane(bodyArea);
		
		
		p.add(searchPanel);
		p.add(Box.createRigidArea(new Dimension(0, 5)));
		p.add(scrollPane);
		p.add(Box.createRigidArea(new Dimension(0, 5)));
		p.add(toPanel);
		p.add(Box.createRigidArea(new Dimension(0, 5)));
		p.add(toScrollPane);
		p.add(Box.createRigidArea(new Dimension(0, 5)));
		p.add(subjectPanel);
		p.add(Box.createRigidArea(new Dimension(0, 5)));
		p.add(subjectTF);
		p.add(bodyPanel);
		p.add(Box.createRigidArea(new Dimension(0, 2)));
		p.add(bodyScrollPane);
		
		sendBtn = new JButton("Send");
		sendBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String receivers = toArea.getText();
				String[] to = receivers.split(", ");
				String subject = subjectTF.getText();
				String bodyText = bodyArea.getText();
				new Email(to, subject, bodyText);
				sendMailFrame.setVisible(false);
			}		
		});
		
		cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				sendMailFrame.setVisible(false);
			}		
		});
		
		btnPanel.add(sendBtn);
		btnPanel.add(cancelBtn);
		
		p.add(btnPanel);
	}
	
	public void showGUI() {
		sendMailFrame = new JFrame();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
		int w = sendMailFrame.getSize().width;
		int h = sendMailFrame.getSize().height;
		int x = (dim.width-w)/8;
		int y = (dim.height-h)/8;
		
		sendMailFrame.setTitle("Send Mail");
		sendMailFrame.setLayout(new FlowLayout());
		sendMailFrame.setLocation(x, y);	
		sendMailFrame.setSize(800, 600);
		sendMailFrame.add(p);
		sendMailFrame.setVisible(true);
	}
	
	class MyTableModel extends AbstractTableModel {

		private static final long serialVersionUID = 1L;
		private List<Member> members = new ArrayList<Member>();

		public MyTableModel(List<Member> members) {
			this.members = members;
		}

		public void addMember(Member m) {
			members.add(m);
			int row = members.size() - 1;
			fireTableRowsInserted(row, row);
		}

		public void removeMemberFromTable(int row) {
			members.remove(row);
			fireTableRowsDeleted(row, row);
		}

		public void tableUpdate(int row, Member m) {
			if (row >= 0 && row < members.size()) {
				members.set(row, m);
				fireTableRowsUpdated(row, row);
			}
		}

		public List<Member> getMembers() {
			return new ArrayList<Member>(members);
		}

		@Override
		public int getColumnCount() {
			return 4;
		}

		@Override
		public int getRowCount() {
			return members.size();
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			Member m = members.get(rowIndex);

			switch (columnIndex) {
			case 0:
				return m.getMemberId();

			case 1:
				return m.getfName();

			case 2:
				return m.getlName();

			case 3:
				return m.getEmail();

			default:
				return "";
			}
		}

		public String getColumnName(int column) {
			switch (column) {
			case 0:
				return "MedlemsID";

			case 1:
				return "Fornavn";

			case 2:
				return "Efternavn";

			case 3:
				return "Email";

			default:
				return "";
			}
		}

		public Class<?> getColumnClass(int columnIndex) {
			switch (columnIndex) {
			case 0:
				return Integer.class;

			case 1:
				return String.class;

			case 2:
				return String.class;

			case 3:
				return String.class;

			default:
				return String.class;
			}
		}
	}
}	
